"""
@function
Set up alias from config.

@detail
This script simply append the aliases read from './my_aliases.json' into file '~/.zshrc'.
"""
import json
import pprint
import os

json_file = 'my_aliases.json'

def read_aliases_from_json(json_file):
    with open(json_file, 'r') as f:
        data = json.load(f)
    return data.get('alias', {})

def append_aliases_to_zshrc(aliases):
    zshrc_path = os.path.expanduser('~/.zshrc')
    with open(zshrc_path, 'a') as zshrc:
        for alias, command in aliases.items():
            zshrc.write(f"alias {alias}='{command}'\n")

def main():
    aliases = read_aliases_from_json(json_file)
    pprint.pprint(aliases)
    if aliases:
        append_aliases_to_zshrc(aliases)
        print(f"There are {len(aliases)} aliases have been added to ~/.zshrc.")
        
        # Unfortunately, we can't effectively execute 'source ~/.zshrc' within a script.
        print("Please run `source ~/.zshrc` to apply the changes.")
    else:
        print("No aliases found in the JSON file.")

if __name__ == "__main__":
    main()
